package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.UserUnLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 200000)
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RecommendUserApi recommendUserApi;

    @Override
    public Long queryEachLoveCount(Long userId) {
        // 查询统计互相喜欢 db.tanhua_users.find({userId:1})
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, Friend.class);
    }

    @Override
    public Long queryLoveCount(Long userId) {
        // 查询统计喜欢 db.user_like.find({userId:1})
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryFanCount(Long userId) {
        // 查询统计粉丝 db.user_like.find({likeUserId:1})
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public PageResult queryEachLoveList(Long userId, Integer page, Integer pagesize) {
        // 互相关注  db.tanhua_users.find({userId:1})
        // 条件
        Query query = Query.query(Criteria.where("userId").is(userId))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        // 分页查询
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);
        // 通过List<Map<String,Object>>封装返回的结果
        List<Map<String,Object>> list = new ArrayList<>();
        if (friendList != null && friendList.size()>0) {
            Map<String,Object> map = null;
            for (Friend friend : friendList) {
                map = new HashMap<>();
                map.put("userId",friend.getFriendId());
                map.put("score",recommendUserApi.queryScore(friend.getUserId(),friend.getFriendId()));
                list.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,list);
    }

    @Override
    public PageResult queryLoveList(Long userId, Integer page, Integer pagesize) {
         // 我关注  db.user_like.find({userId:1})
        Query query = Query.query(Criteria.where("userId").is(userId))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        // 通过List<Map<String,Object>>封装返回的结果
        List<Map<String,Object>> list = new ArrayList<>();
        if (userLikeList != null && userLikeList.size()>0) {
            Map<String,Object> map = null;
            for (UserLike userLike : userLikeList) {
                map = new HashMap<>();
                map.put("userId",userLike.getLikeUserId());
                map.put("score",recommendUserApi.queryScore(userLike.getUserId(),userLike.getLikeUserId()));
                list.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,list);
    }

    @Override
    public PageResult queryFanList(Long userId, Integer page, Integer pagesize) {
        // 粉丝  db.user_like.find({likeUserId:1})
        Query query = Query.query(Criteria.where("likeUserId").is(userId))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        // 通过List<Map<String,Object>>封装返回的结果
        List<Map<String,Object>> list = new ArrayList<>();
        if (userLikeList != null && userLikeList.size()>0) {
            Map<String,Object> map = null;
            for (UserLike userLike : userLikeList) {
                map = new HashMap<>();
                map.put("userId",userLike.getUserId());
                map.put("score",recommendUserApi.queryScore(userLike.getLikeUserId(),userLike.getUserId()));
                list.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,list);
    }

    @Override
    public PageResult queryVistorsList(Long userId, Integer page, Integer pagesize) {
        // 谁看过我 db.visitors.find({userId:1}).sort({date:-1})
        Query query = Query.query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);
        long count = mongoTemplate.count(query, Visitors.class);

        // 通过List<Map<String,Object>>封装返回的结果
        List<Map<String,Object>> list = new ArrayList<>();
        if (visitorsList != null && visitorsList.size()>0) {
            Map<String,Object> map = null;
            for (Visitors visitors : visitorsList) {
                map = new HashMap<>();
                map.put("userId",visitors.getVisitorUserId());
                map.put("score",recommendUserApi.queryScore(visitors.getUserId(),visitors.getVisitorUserId()));
                list.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,list);
    }

    @Override
    public void delete(Long userId, Long likeUserId) {
        //* 删除粉丝中的喜欢数据 db.user_like.remove({likeUserId:1,userId:23})
        Query query = new Query(
            Criteria.where("userId").is(likeUserId)
                .and("likeUserId").is(userId)
        );
        mongoTemplate.remove(query,UserLike.class);
    }

    @Override
    public void save(Long userId, Long likeUserId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("likeUserId").is(likeUserId)
        );
        if (mongoTemplate.findOne(query, UserLike.class) == null) {
            UserLike userLike = new UserLike();
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            userLike.setCreated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
        }
    }

    @Override
    public UserLike find(Long userId, Long likeUserId) {
        Query query = new Query(
                Criteria.where("userId").is(likeUserId)
                        .and("likeUserId").is(userId)
        );
        UserLike user = mongoTemplate.findOne(query, UserLike.class);
        if (user != null) {
            return user;
        }
        return null;
    }

    @Override
    public void saveUnLike(Long userId, Long unLikeUserId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("unLikeUserId").is(unLikeUserId)
        );
        if (mongoTemplate.findOne(query, UserUnLike.class) == null) {
            UserUnLike userUnLike = new UserUnLike();
            userUnLike.setUserId(userId);
            userUnLike.setUnLikeUserId(unLikeUserId);
            userUnLike.setCreated(System.currentTimeMillis());
            mongoTemplate.save(userUnLike);
        }
    }

    @Override
    public List<UserLike> findLikeId(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);

        return userLikes;
    }

    @Override
    public List<UserUnLike> findUnLikeId(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserUnLike> userUnLikes = mongoTemplate.find(query, UserUnLike.class);
        return userUnLikes;
    }
}
